﻿using DevExpress.Xpf.Bars.Native;
using FastMember;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using TeBigCommonUI.Common;

namespace TeBigCommonUI.Items
{
    public class BaseBiBarControl : BaseItemControl
    {
        public string ArgumentDataMember
        {
            get { return (string)GetValue(ArgumentDataMemberProperty); }
            set { SetValue(ArgumentDataMemberProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ArgumentDataMember.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ArgumentDataMemberProperty =
            DependencyProperty.Register("ArgumentDataMember", typeof(string), typeof(BaseBiBarControl), new PropertyMetadata(""));

        public string ValueDataMember
        {
            get { return (string)GetValue(ValueDataMemberProperty); }
            set { SetValue(ValueDataMemberProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ValueDataMember.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueDataMemberProperty =
            DependencyProperty.Register("ValueDataMember", typeof(string), typeof(BaseBiBarControl), new PropertyMetadata(""));

        public IEnumerable DataSource
        {
            get { return (IEnumerable)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DataSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(IEnumerable), typeof(BaseBiBarControl), 
                new PropertyMetadata(OnDataSourceChanged));


        private static void OnDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is BiBarControl bibar)
            {
                if (e.OldValue is INotifyCollectionChanged old_value)
                {
                    old_value.CollectionChanged -= bibar.OnDataSourceCollectionChanged;
                }

                if (e.NewValue is INotifyCollectionChanged new_value)
                {
                    new_value.CollectionChanged += bibar.OnDataSourceCollectionChanged;
                }
            }
        }
    }

    public partial class BiBarControl : BaseBiBarControl
    {
        public BiBarControl()
        {
            InitializeComponent();
        }

        internal void OnDataSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            item_control.UpdateTarget();
        }

        private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var items = TreeHelper.GetChildren<ScrollViewer>(grid_main);

            foreach (var item in items)
            {
                item.ScrollToVerticalOffset(item.VerticalOffset - e.Delta);
            }

            e.Handled = true;
        }

    }

    public class BiBarValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length == 2)
            {
                if (values[0] != null && this.IsValidValue(values[0]) && values[1] is string key)
                {
                    object v;

                    switch (values[0])
                    {
                        case DataRowView row_view:
                            v = row_view[key];
                            break;
                        case DataRow row:
                            v = row[key];
                            break;
                        default:
                            v = values[0].FastGetProperty(key);
                            break;
                    }

                    if (targetType == typeof(double))
                        return System.Convert.ToDouble(v);

                    if (targetType == typeof(string))
                        return System.Convert.ToString(v);

                    return v;
                }
            }

            return null;
        }


        public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}